import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login.vue'
import Main from '../components/Main.vue'
import Register from "../components/Register";
import CodeSubmit from "../components/CodeSubmit";
import ElementUI from 'element-ui';

Vue.use(Router)
Vue.use(ElementUI)
const router=new Router({
  routes: [
    {
      path: '/', redirect: '/login'
    },
    {
      path: '/login', component: Login
    },
    {
      path: '/main', component: Main
    },{
      path: '/register', component: Register
    },{
      path: '/codeSubmit', component: CodeSubmit
    }
  ]
})
router.beforeEach((to,from,next) =>{
  if (to.path === '/login' || to.path=== '/register') return next()
  //获取token
  const tokenStr =window.sessionStorage.getItem('token')
  //token为空返回登录页
  if(!tokenStr) return next('/login')
  next()
})

export default router


